sda = 3
scl = 4
DATE = "wait..."
WEEK = ".."
TIMES = {
  HH = 0, 
  MM = 0, 
  SS = 0
}
RESPONSE = nil
WIFI_LIST = {
  {
    ssid = "Uso P40", 
    pwd = "46369a3f5db7" 
  }

}
function start()
  init_OLED(sda,scl)
  print_OLED(DATE, TIMES)
  connectWIFI()
end

function update()
  if not RESPONSE then return end
  TIMES.SS = TIMES.SS + 1
  -- MM + 1
  if TIMES.SS >= 60 then
    TIMES.MM = TIMES.MM + 1
    TIMES.SS = 0
    -- HH + 1
    if TIMES.MM >= 60 then
      TIMES.HH = TIMES.HH + 1
      TIMES.MM = 0
      -- DD
      if TIMES.HH >= 24 then
        TIMES.HH = 0
        local date = Split(DATE, "-")
        local yy, mm, dd = date[1],  date[2], date[3]
        -- Month + 1
        if get_days_in_month(mm, yy) < dd+1 then
          mm = mm + 1
          dd = 1
          -- year + 1
          if mm > 12 then
            yy = yy + 1
            mm = 1
          end
        end
        DATE = yy.."-"..mm.."-"..dd
        _, WEEK = get_day_of_week(yy, mm, dd)
      end
    end
  end
  print_OLED(DATE, TIMES)
end

wifiListIndex = nil
-- connect WIFI
function connectWIFI()
--    wifi.setmode(wifi.STATIONAP)
--    local timer_connect = tmr.create()
--    timer_connect:alarm( 5000, 1, function()
--        if wifi.sta.getip() then
--            timer_connect:stop()
--            getTimeRequest()
--            return
--        end
--        local cfg = nil
--        wifiListIndex,cfg = next(WIFI_LIST, wifiListIndex)
--        if not cfg then
--            timer_connect:stop()
--            return
--        end
--        wifi.sta.config(cfg)
--        wifi.sta.connect()
--        cfg = nil
--    end)
wifi.sta.autoconnect(1)

local timer_connect = tmr.create()
timer_connect:alarm( 1000, 1, function()
    if wifi.sta.getip() then
        getTimeRequest()
        timer_connect:stop()
    end
end)
end
timeout = 0
-- get date on network
function getTimeRequest()

  local timer_http = tmr.create()
  timer_http:alarm( 1000, 1, function()
    if wifi.sta.getip() then
      http.get("http://quan.suning.com/getSysTime.do",
      'Content-Type: text/html; charset=UTF-8',
      function (code, data)
        if code == 200 then
          print(code,  data)
          -- data : {"sysTime2":"2021-06-29 17:14:37","sysTime1":"20210629171437"}
          RESPONSE = sjson.decode (data)
          DATE = Split(RESPONSE.sysTime2,  " ")
          times = Split(DATE[2],  ":")
          -- get time
          TIMES.HH = times[1]
          TIMES.MM = times[2]
          TIMES.SS = times[3]
          -- get date
          DATE = DATE[1]
          -- get week
          data = Split(DATE, "-")
          local yy, mm, dd = data[1],  data[2], data[3]
          _, WEEK = get_day_of_week(yy, mm, dd)
          -- stop
          timer_http:stop()
        end
      end)
      if timeout >= 9 then timer_http:stop() end
      timeout = timeout + 1
    end
  end)
end

function Split(szFullString, szSeparator)
local nFindStartIndex = 1
local nSplitIndex = 1
local nSplitArray = {}
while true do
   local nFindLastIndex = string.find(szFullString, szSeparator, nFindStartIndex)
   if not nFindLastIndex then
    nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, string.len(szFullString))
    break
   end
   nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, nFindLastIndex - 1)
   nFindStartIndex = nFindLastIndex + string.len(szSeparator)
   nSplitIndex = nSplitIndex + 1
end
return nSplitArray
end

function get_days_in_month(month, year)
  local function is_leap_year(year)
    return year % 4 == 0 and (year % 100 ~= 0 or year % 400 == 0)
  end
  return month == 2 and is_leap_year(year) and 29
         or ("\31\28\31\30\31\30\31\31\30\31\30\31"):byte(month)
end

function get_day_of_week(dd, mm, yy) 
  local days = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }

  local mmx = mm

  if (mm == 1) then  mmx = 13; yy = yy-1  end
  if (mm == 2) then  mmx = 14; yy = yy-1  end

  local val8 = dd + (mmx*2) +  math.floor(((mmx+1)*3)/5)   + yy + math.floor(yy/4)  - math.floor(yy/100)  + math.floor(yy/400) + 2
  local val9 = math.floor(val8/7)
  local dw = val8-(val9*7) 

  if (dw == 0) then
    dw = 7
  end

  return (dw+6)%8+1, days[dw]
end

function init_OLED(sda,scl)
  sla = 0x3c
  i2c.setup(0, sda, scl, i2c.SLOW)
  disp=u8g2.ssd1306_i2c_128x64_noname(0,sla)
  disp:setFont(u8g2.font_unifont_t_symbols)
  disp:setFontPosTop()
end
function print_OLED(date, TIMES)
  disp:clearBuffer()
  disp:drawFrame(0, 0,128,64)
  disp:setFont(u8g2.font_unifont_t_symbols)
  disp:drawStr(10, 1, date)
  disp:drawStr(100, 20, WEEK)
  disp:setFont(u8g2.font_fub25_tn)
  disp:drawStr(10, 20, string.format("%.2d:%.2d", TIMES.HH or 0, TIMES.MM or 0))
  disp:setFont(u8g2.font_unifont_t_symbols)
  disp:drawStr(100, 32, string.format("%.2d", TIMES.SS or 0))
--  disp:drawStr(50, 40, "你好:uso")
--  disp:drawFrame(0, 16,128,45)
  disp:sendBuffer()
end
